package pingan.com.imandtwo.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.activity.AddFriendActivity;
import pingan.com.imandtwo.fragments.addressbookfragment.FriendFragment;
import pingan.com.imandtwo.fragments.addressbookfragment.GroupFragment;
import pingan.com.imandtwo.fragments.addressbookfragment.ModileFragment;


/**
 * Created by xsj on 2018/2/1.
 * 这是通讯录
 */

public class AddressbookFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.pager)
    ViewPager pa;
    @BindView(R.id.newfriend)
    RelativeLayout newfriend;
    @BindView(R.id.rg1)
    RadioButton rg1;
    @BindView(R.id.rg2)
    RadioButton rg2;
    @BindView(R.id.rg3)
    RadioButton rg3;

    private List<Fragment> data = new ArrayList<Fragment>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.addressbookfragment, container, false);

        unbinder = ButterKnife.bind(this, view);
        //添加
        data.add(new ModileFragment());
        data.add(new FriendFragment());
        data.add(new GroupFragment());
        //监听事件
        pa.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                //改变radiobutton选中状态
                rg.check(rg.getChildAt(arg0).getId());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

            }
        });

//设置适配器
        Myadpader myadpader = new Myadpader(getActivity().getSupportFragmentManager());
        pa.setAdapter(myadpader);
//点击事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rg1:
                        pa.setCurrentItem(0);
                        break;
                    case R.id.rg2:
                        pa.setCurrentItem(1);
                        break;
                    case R.id.rg3:
                        pa.setCurrentItem(2);
                        break;

                    default:
                        break;
                }

            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.newfriend, R.id.rg1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.newfriend:
                Intent intent = new Intent(getActivity(), AddFriendActivity.class);
                startActivity(intent);
                break;
        }
    }


    //适配器
    public class Myadpader extends FragmentPagerAdapter {


        public Myadpader(FragmentManager fm) {
            super(fm);
// TODO Auto-generated constructor stub
        }


        @Override
        public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
            return data.get(arg0);
        }


        @Override
        public int getCount() {
// TODO Auto-generated method stub
            return data.size();
        }

    }


}
